# -*- coding:utf-8 -*-

# import ssl
# ssl._create_default_https_context =ssl._create_stdlib_context # 解决Mac开发环境下，网络错误的问题

from celery_tasks.sms.yuntongxun.CCPRestSDK import REST

# 说明：主账号，登陆云通讯网站后，可在"控制台-应用"中看到开发者主账号ACCOUNT SID
_accountSid = '8aaf07086e0115bb016e873e16fe4da6'

# 说明：主账号Token，登陆云通讯网站后，可在控制台-应用中看到开发者主账号AUTH TOKEN
_accountToken = '0f59d259bae548a390fe1607d38a6f23'

# 请使用管理控制台首页的APPID或自己创建应用的APPID
_appId = '2c9488768610eb8001861c14ccae02b1'

# 说明：请求地址，生产环境配置成app.cloopen.com
_serverIP = 'sandboxapp.cloopen.com'

# 说明：请求端口 ，生产环境为8883
_serverPort = "8883"

# 说明：REST API版本号保持不变
_softVersion = '2013-12-26'

# 云通讯官方提供的发送短信代码实例
# 发送模板短信
# @param to 手机号码
# @param datas 内容数据 格式为数组 例如：{'12','34'}，如不需替换请填 ''
# @param $tempId 模板Id
# def sendTemplateSMS(to, datas, tempId):
#     # 初始化REST SDK
#     rest = REST(_serverIP, _serverPort, _softVersion)
#     rest.setAccount(_accountSid, _accountToken)
#     rest.setAppId(_appId)
#
#     result = rest.sendTemplateSMS(to, datas, tempId)
#     # {'statusCode': '000000', 'templateSMS': {'smsMessageSid': '2363bca1a6ad4a3a871dd4fa9f56e2bc', 'dateCreated': '20230614112824'}}
#     print(result)
#     # result.get("statusCode")=="000000":短信发送成功的状态码
#     # result.get("statusCode")=="112310"：向未登记过的手机号发送短信验证码时的状态码
#     # result.get("statusCode")=="121002"：余额不足时返回的状态码
#     if result.get("statusCode")=="000000" \
#             or result.get("statusCode")=="112310"\
#             or result.get("statusCode")=="121002":
#         return 0
#     else:
#         return -1


class CPP:
    def __new__(cls, *args, **kwargs):
        """
        在我们创建对象时即为对象分配空间时调用当前方法
        :param args:
        :param kwargs:
        :return: 单例类对象
        """
        # hasattr(CPP,"_instance"):判断CPP类中是否有属性_instance，有返回True,没有返回False
        if not hasattr(CPP,"_instance"):
            # super().__new__(cls,*args,**kwargs):对对象分配内存空间
            cls._instance = super().__new__(cls,*args,**kwargs)
            # 初始化REST SDK
            cls._instance.rest = REST(_serverIP, _serverPort, _softVersion)
            cls._instance.rest.setAccount(_accountSid, _accountToken)
            cls._instance.rest.setAppId(_appId)

        print("__new__(cls, *args, **kwargs)")
        return cls._instance

    def sendTemplateSMS(self,to, datas, tempId):
        result = self.rest.sendTemplateSMS(to, datas, tempId)
        # {'statusCode': '000000', 'templateSMS': {'smsMessageSid': '2363bca1a6ad4a3a871dd4fa9f56e2bc', 'dateCreated': '20230614112824'}}
        print(result)
        # result.get("statusCode")=="000000":短信发送成功的状态码
        # result.get("statusCode")=="112310"：向未登记过的手机号发送短信验证码时的状态码
        # result.get("statusCode")=="121002"：余额不足时返回的状态码
        if result.get("statusCode") == "000000" \
                or result.get("statusCode") == "112310" \
                or result.get("statusCode") == "121002":
            return 0
        else:
            return -1



if __name__ == '__main__':

    result = CPP().sendTemplateSMS("15033889442",["1234",5],1)
    print("result=",result)

    # cpp1 = CPP()
    # cpp2 = CPP()
    # print(cpp1)
    # print(cpp2)
    # print(cpp1==cpp2)

    # 注意： 测试的短信模板编号为1
    # result = sendTemplateSMS('15033889442', ['1234', 5], 1)
    # print("result=",result)
